const axios = require('axios');

/**
 * 发送请求到指定URL并返回响应数据
 * @param {string} cookie - 请求的Cookie字符串
 * @param {string} xS - 请求头中的x-s字段
 * @returns {Promise} - 返回一个Promise，解析为响应数据
 */

// 使用示例
const defaultCookie = `abRequestId=aec348ac-4390-573f-9d93-617af756f36a; xsecappid=xhs-pc-web; a1=1943c266ddf1b46edpaex7hy4cvmdc51kmir3pai550000347772; webId=ff097b6629199cc3d5e40bdada7276c2; gid=yj4qSJKW4STYyj4qSJKKf6U0fiyD4KdfU0dC332Wxv4ShF286fS2yq888q4WWWJ8JJddj8J8; web_session=040069b17c2ea0561c80474fb1354b9e1daf1e; webBuild=4.55.1; acw_tc=0a0b135b17373953807808722e22d1339491c7aa2403653741f3f8eb4ef5da; websectiga=8886be45f388a1ee7bf611a69f3e174cae48f1ea02c0f8ec3256031b8be9c7ee; sec_poison_id=69fdf5cd-9d6a-4847-9c87-97ad43acac68; unread={%22ub%22:%22678e7169000000001800abff%22%2C%22ue%22:%22676a5a33000000000b0179c6%22%2C%22uc%22:13}`;
const defaultXS = 'XYW_eyJzaWduU3ZuIjoiNTYiLCJzaWduVHlwZSI6IngyIiwiYXBwSWQiOiJ4aHMtcGMtd2ViIiwic2lnblZlcnNpb24iOiIxIiwicGF5bG9hZCI6ImQ1NTE2MTMxYWIzYWJjNjAyNzU1N2RmNzBkMTg2MGZiY2U5NGUxNjNiMjBlNjg0NmVkNTQwYzY1NjE3M2ZkMjQzZDc4Mjg5MmJiNTVlNjVlZmFkNTgxZTUwODQ4MmI1ODg2MzIyYTYxYjllOWM1ZTJlYjc1M2M5YWZlM2I2ODYzYmJmNzgxOTA1M2JlOTc4ZDNlMjg3NzRjNWI3NTMwZTA5Yzk4ODQxNjJmZmI0ZmY1ZjBlNjU1MTJhNjdiMzIzNTVlNmNhNDBkOTEzOWMxMDNmMmQxOGE0Njc2ZTBhZDhmMTY1NWU0ZDhhYTBkYWRmZTY3MGE4MDkyM2M2ZTEyZTMyYWQ3NTFmOWY3ODljM2M5OGMzZWNkMTAzMDFjNWU5Y2Q1MDQ1MjAzYWRjYjUyMDMyZDQ3MzRmZWJhODNhMGVmZmZjZjk1Zjc0NGRhZmU3OWVhNTlkMTViNmEyNmZkMmNjMmY3YmM3ODRhNzNiN2JkNTI0OWQ2OGQyNWJkMTcxOTc4YzEzMjJmMDdkYjU4Y2UyYmI1NjgyMTk0M2NiYTg3In0';


async function fetchData(cookie = defaultCookie, xS = defaultXS) {
    const axios = require('axios');
    let data = '{"cursor_score":"1.7373969269790015E9","num":21,"refresh_type":3,"note_index":20,"unread_begin_note_id":"","unread_end_note_id":"","unread_note_count":0,"category":"homefeed_recommend","search_key":"","need_num":6,"image_formats":["jpg","webp","avif"],"need_filter_image":false}';

    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: 'https://edith.xiaohongshu.com/api/sns/web/v1/homefeed',
        headers: {
            'accept': 'application/json, text/plain, */*',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cache-control': 'no-cache',
            'content-type': 'application/json;charset=UTF-8',
            cookie: cookie,
            // 'cookie': 'abRequestId=aec348ac-4390-573f-9d93-617af756f36a; xsecappid=xhs-pc-web; a1=1943c266ddf1b46edpaex7hy4cvmdc51kmir3pai550000347772; webId=ff097b6629199cc3d5e40bdada7276c2; gid=yj4qSJKW4STYyj4qSJKKf6U0fiyD4KdfU0dC332Wxv4ShF286fS2yq888q4WWWJ8JJddj8J8; web_session=040069b17c2ea0561c80474fb1354b9e1daf1e; webBuild=4.55.1; acw_tc=0a0b135b17373953807808722e22d1339491c7aa2403653741f3f8eb4ef5da; websectiga=8886be45f388a1ee7bf611a69f3e174cae48f1ea02c0f8ec3256031b8be9c7ee; sec_poison_id=69fdf5cd-9d6a-4847-9c87-97ad43acac68; unread={%22ub%22:%22678e7169000000001800abff%22%2C%22ue%22:%22676a5a33000000000b0179c6%22%2C%22uc%22:13}',
            'origin': 'https://www.xiaohongshu.com',
            'pragma': 'no-cache',
            'x-s': xS
            // 'x-s': 'XYW_eyJzaWduU3ZuIjoiNTYiLCJzaWduVHlwZSI6IngyIiwiYXBwSWQiOiJ4aHMtcGMtd2ViIiwic2lnblZlcnNpb24iOiIxIiwicGF5bG9hZCI6ImQ1NTE2MTMxYWIzYWJjNjAyNzU1N2RmNzBkMTg2MGZiY2U5NGUxNjNiMjBlNjg0NmVkNTQwYzY1NjE3M2ZkMjQzZDc4Mjg5MmJiNTVlNjVlZmFkNTgxZTUwODQ4MmI1ODg2MzIyYTYxYjllOWM1ZTJlYjc1M2M5YWZlM2I2ODYzYmJmNzgxOTA1M2JlOTc4ZDNlMjg3NzRjNWI3NTMwZTA5Yzk4ODQxNjJmZmI0ZmY1ZjBlNjU1MTJhNjdiMzIzNTVlNmNhNDBkOTEzOWMxMDNmMmQxOGE0Njc2ZTBhZDhmMTY1NWU0ZDhhYTBkYWRmZTY3MGE4MDkyM2M2ZTEyZTMyYWQ3NTFmOWY3ODljM2M5OGMzZWNkMTAzMDFjNWU5Y2Q1MDQ1MjAzYWRjYjUyMDMyZDQ3MzRmZWJhODNhMGVmZmZjZjk1Zjc0NGRhZmU3OWVhNTlkMTViNmEyNmZkMmNjMmY3YmM3ODRhNzNiN2JkNTI0OWQ2OGQyNWJkMTcxOTc4YzEzMjJmMDdkYjU4Y2UyYmI1NjgyMTk0M2NiYTg3In0='
        },
        data: data
    };
    try {
        const res = await axios.request(config)
            .then((response) => {
                // console.log(JSON.stringify(response.data));
                return response.data;
            })
            .catch((error) => {
                console.log(error);
            });

        const info = res.data.items.filter(item => item.note_card.display_title.includes("房"))
        return info;
    } catch (e) {
        return []
    }

}


module.exports = {
    fetchData
};

